<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>ZDH 在线标签新增值首页</title>
    <meta name="keywords" content="ZDH 在线标签新增值首页">
    <meta name="description" content="ZDH 在线标签新增值首页">

    <link rel="shortcut icon" href="img/favicon.ico">

    <link href="css/plugins/chosen/chosen.css" rel="stylesheet">
    <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css?v=4.1.0" rel="stylesheet">
    <link href="css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">

    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>标签配置</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="dropdown-toggle" data-toggle="dropdown" href="buttons.html#">
                            <i class="fa fa-eye"></i>
                        </a>
                        <ul class="dropdown-menu dropdown-user">
                            <li><a href="javascript:void(0);" onclick="getResourceDesc()">功能说明</a>
                            </li>
                        </ul>
                    </div>
                </div>
                <div class="ibox-content">

                    <form id="label_add_form" name="label_add_form" method="post" class="form-horizontal"
                          action="">

                        <div class="form-group">
                            <label class="col-sm-2 control-label">产品</label>
                            <div class="col-sm-10">
                                <select id="product_code" name="product_code"
                                        data-placeholder="产品code...."
                                        class="chosen-select form-control m-b" >
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">标签说明</label>
                            <div class="col-sm-10">
                                <input id="label_context" name="label_context" type="text"
                                       placeholder="标签说明" class="form-control" aria-required="true"> </span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">使用方式</label>
                            <div class="col-sm-10">
                                <select id="label_use_type" name="label_use_type"
                                        data-placeholder="选择标签使用方式..."
                                        class="chosen-select form-control m-b" tabindex="2">
                                    <option value="" mytype="">空</option>
                                    <option value="batch" mytype="">值查人(批量)</option>
                                    <option value="single" mytype="">人查值(单个)</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">标签类别</label>
                            <div class="col-sm-10">
                                <select id="label_type" name="label_type"
                                        data-placeholder="选择标签类型..."
                                        class="chosen-select form-control m-b" tabindex="2">
                                    <option value="" mytype="">选择标签类型...</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">标签标识</label>
                            <div class="col-sm-10">
                                <input id="label_code" name="label_code" type="text"
                                       placeholder="标签标识" class="form-control" aria-required="true"> </span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">账号</label>
                            <div class="col-sm-10">
                                <input id="uid" name="uid" type="text"
                                       placeholder="账号" class="form-control" aria-required="true"> </span>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">创建时间</label>
                            <div class="col-sm-10">
                                <input id="create_time" name="create_time" type="text" readonly
                                       placeholder="创建时间" class="form-control" aria-required="true"> </span>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-2" ></div>
                        </div>

                        <div class="hr-line-dashed"></div>

                        <div class="form-group" id="label_div">

                        </div>


                        <div class="hr-line-dashed"></div>
                        <!-- 输出数据源结束-->

                        <div class="form-group">
                            <div class="text-center">
                                <button id="update_etl_task" name="save_zdh" class="btn btn-primary"
                                        οnsubmit='return false'
                                        type="button">更新
                                </button>
                                <button id="reset" class="btn btn-white" type=reset>清空</button>
                            </div>
                        </div>


                    </form>


                </div>
            </div>


        </div>
    </div>
</div>


<script src="js/zdh_common.js"></script>
<!-- 全局js -->
<script src="js/jquery.min.js?v=2.1.4"></script>
<script src="js/bootstrap.min.js?v=3.3.6"></script>

<!-- Bootstrap table -->
<script src="js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
<script src="js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<!-- 自定义js -->
<script src="js/content.js?v=1.0.0"></script>

<!-- Chosen -->
<script src="js/plugins/chosen/chosen.jquery.js"></script>

<!-- layer javascript -->
<script src="js/plugins/layer/layer.min.js"></script>

<!-- iCheck -->
<script src="js/plugins/iCheck/icheck.min.js"></script>

<script src="js/admin/dim_product_common.js"></script>
<script>


    var template = '<div class="from-group col-sm-12"  id="template">\n' +
        '<label class="col-sm-1 control-label">参数</label>\n' +
        '        <div class="col-sm-2" >\n' +
        '            <input name="param_code" type="text"\n' +
        '                   placeholder="参数code" class="form-control param_code" aria-required="true" value="zdh_param_code">\n' +
        '        </div>\n' +
        '        <div class="col-sm-2">\n' +
        '            <input name="param_context" type="text"\n' +
        '                   placeholder="参数说明" class="form-control param_context" aria-required="true" value="zdh_param_context">\n' +
        '        </div>\n' +
        '        <div class="col-sm-2">\n' +
        '            <select class="form-control m-b param_type" data-live-search="true" name="param_type" id="zdh_param_type">\n' +
        '<option value="string">字符串</option>\n'+
        '<option value="int">数值</option>\n'+
        '<option value="long">数值(10亿以上)</option>\n'+
        '<option value="double">小数</option>\n'+
        '<option value="boolean">布尔</option>\n'+
        '<option value="date">日期</option>\n'+
        '<option value="timestamp">日期timestamp</option>\n'+
        '<option value="ts">日期时间戳</option>\n'+
        '            </select>\n' +
        '        </div>\n' +
        '        <div class="col-sm-2">\n' +
        '            <input name="param_value" type="text"\n' +
        '                   placeholder="参数值,多个分号分割" class="form-control param_value" aria-required="true" value="zdh_param_value">\n' +
        '        </div>\n' +

        '\n' +
        // '    </div>\n' +
        '</div>';

    $(document).ready(function () {
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
        });
    });

    (function (document, window, $) {


        (function () {

            var url = location.search; //这一条语句获取了包括问号开始到参数的最后，不包括前面的路径
            var params = url.substr(1);//去掉问号
            var pa = params.split("&");
            var s = new Object();//定义一个全局变量-存储任务ID
            for (var i = 0; i < pa.length; i++) {
                s[pa[i].split("=")[0]] = unescape(pa[i].split("=")[1]);
            }

            $("#btn_enum").click(function () {
                var tmp = template;
                tmp = tmp.replace("zdh_param_code", "");
                tmp = tmp.replace("zdh_param_context", "");
                tmp = tmp.replace("zdh_param_type", "");
                tmp = tmp.replace("zdh_param_value", "");
                $('#label_div').append(tmp);
                $('#label_div #delete').click(function () {
                    $(this).parent().parent().remove();
                });
            });
            $('#label_type').chosen();
            $('#label_use_type').chosen();
            $('#label_data_time_effect').chosen();
            $('#label_data_update_type').chosen();

            function init_label_type() {
                $.ajax({
                    type: 'POST',
                    url: server_context+"/enum_by_code",
                    dataType: 'json',
                    async: false,
                    data: {'enum_code': 'label_type'},
                    //成功返回
                    success: function (data) {
                        if(data.code != "200"){
                            layer.msg(data.msg);
                            return ;
                        }
                        var str = '<option value=\"\" mytype=\"\" hassubinfo=\"true\"></option>';
                        for (var i = 0; i < data.result.enum_json_object.length; i++) {
                            str += '<option value=\"' + data.result.enum_json_object[i].enum_value + "\" mytype=\"" + data.result.enum_json_object[i].enum_value + "\" hassubinfo=\"true\">" + data.result.enum_json_object[i].enum_value_context + "</option>"
                        }
                        //输入数据源类型下拉框
                        $('#label_type').html(str);
                        $("#label_type").trigger("chosen:updated");
                        $('#label_type').chosen();

                    },
                    //处理完成
                    complete: function () {
                        console.info("complete")
                    },
                    //报错
                    error: function (data) {
                        console.info("error: " + data.responseText);
                    }
                });
            }

            function init_label_engine() {
                $.ajax({
                    type: 'POST',
                    url: server_context+"/enum_by_code",
                    dataType: 'json',
                    async: false,
                    data: {'enum_code': 'label_engine'},
                    //成功返回
                    success: function (data) {
                        if(data.code != "200"){
                            layer.msg(data.msg);
                            return ;
                        }
                        var str = '<option value=\"\" mytype=\"\" hassubinfo=\"true\"></option>';
                        for (var i = 0; i < data.result.enum_json_object.length; i++) {
                            str += '<option value=\"' + data.result.enum_json_object[i].enum_value + "\" mytype=\"" + data.result.enum_json_object[i].enum_value + "\" hassubinfo=\"true\">" + data.result.enum_json_object[i].enum_value_context + "</option>"
                        }
                        //输入数据源类型下拉框
                        $('#label_engine').html(str);
                        $("#label_engine").trigger("chosen:updated");
                        $('#label_engine').chosen();

                    },
                    //处理完成
                    complete: function () {
                        console.info("complete")
                    },
                    //报错
                    error: function (data) {
                        console.info("error: " + data.responseText);
                    }
                });
            }

            init_label_type();
            init_label_engine();

            //s.id=-1 表示新增,否则表示更新
            if (s.id == -1) {
                console.info("新增按钮触发");
                $('#update_etl_task').hide();
                $('#save_etl_task').show();

            } else {

                if (s.is_copy == "true") {
                    console.info("拷贝按钮触发");
                    $('#update_etl_task').hide();
                    $('#save_etl_task').show();
                } else {
                    console.info("更新按钮触发");
                    $('#save_etl_task').hide();
                    $('#update_etl_task').show();
                }

                if (s.only_read == "true") {
                    $('#update_etl_task').hide();
                    $('#save_etl_task').hide();
                    $('#reset').hide();
                }
                buildParam();
            }

            function getMyDate(str) {
                var oDate = new Date(str),
                    oYear = oDate.getFullYear(),
                    oMonth = oDate.getMonth() + 1,
                    oDay = oDate.getDate(),
                    oHour = oDate.getHours(),
                    oMin = oDate.getMinutes(),
                    oSen = oDate.getSeconds(),
                    oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay) + " " + getzf(oHour) + ":" + getzf(oMin) + ":" + getzf(oSen);//最后拼接时间
                return oTime;
            };

            //补0操作
            function getzf(num) {
                if (parseInt(num) < 10) {
                    num = '0' + num;
                }
                return num;
            }

            function buildConfig(i,param_code, param_context,param_type, param_operate, param_value, param_enum_key) {
                if(is_empty(param_enum_key)){
                    param_enum_key="0";
                }
                if(is_empty(param_code)){
                    param_code="";
                }
                if(is_empty(param_context)){
                    param_context="";
                }if(is_empty(param_value)){
                    param_value="";
                }
                var tmp=template;
                tmp = tmp.replace("zdh_param_code", param_code);
                tmp = tmp.replace("zdh_param_context", param_context);
                tmp = tmp.replace("zdh_param_value", param_value);
                tmp = tmp.replace("zdh_param_type", "zdh_param_type"+i);
                $('#label_div').append(tmp);
                $("#zdh_param_operate"+i).val(param_operate);
                $("#zdh_param_type"+i).val(param_type);
                $('#label_div #delete').click(function () {
                    $(this).parent().parent().remove();
                });

            }
            function buildParam() {
                if (s.id != '-1') {
                    console.info("更新按钮触发--开始赋值对应参数");
                    //开始赋值

                    $.ajax({
                        url: server_context+"/label_detail",
                        data: "id=" + s.id,
                        type: "post",
                        async: false,
                        dataType: "json",
                        success: function (data) {
                            console.info("success");
                            // layer.alert(JSON.stringify(data[0]))

                            var product_code = data.result.product_code;
                            $("#product_code").val(product_code);
                            $("#product_code").trigger("chosen:updated");

                            //获取ETL 说明
                            var label_context = data.result.label_context;
                            $("#label_context").val(label_context);

                            var label_type = data.result.label_type;
                            $("#label_type").val(label_type);
                            $("#label_type").trigger("chosen:updated");

                            var label_use_type = data.result.label_use_type;
                            $("#label_use_type").val(label_use_type);
                            $("#label_use_type").trigger("chosen:updated");



                            var label_code = data.result.label_code;
                            $("#label_code").val(label_code);
                            $("#label_code").attr("readonly","readonly");

                            var create_time = data.result.create_time;
                            $("#create_time").val(getMyDate(create_time));

                            //解析json
                            if(data.result.param_json_object.length>0){
                                for(var i=0;i<data.result.param_json_object.length;i++){
                                    buildConfig(i,data.result.param_json_object[i].param_code, data.result.param_json_object[i].param_context, data.result.param_json_object[i].param_type,data.result.param_json_object[i].param_operate,
                                        data.result.param_json_object[i].param_value, data.result.param_json_object[i].param_enum_key);
                                }
                            }

                        },
                        error: function (data) {
                            console.info("error: " + data.responseText);
                        }

                    });


                }
            }

        })();

    })(document, window, jQuery);



    $('#update_etl_task').click(function () {

        if ($('#label_context').val() == '') {
            layer.msg("标签说明不能为空！");
            return;
        }


        var url = location.search; //这一条语句获取了包括问号开始到参数的最后，不包括前面的路径
        var params = url.substr(1);//去掉问号
        var pa = params.split("&");
        var s = new Object();
        for (var i = 0; i < pa.length; i++) {
            s[pa[i].split("=")[0]] = unescape(pa[i].split("=")[1]);
        }

        $.ajax({
            url: server_context+"/label_online_add_value",
            type: 'post',
            dataType: "json",
            async: 'false',
            data: $("#label_add_form").serialize()+ "&" + "id=" + s.id,
            //发送数据前
            beforeSend: function () {
                // 禁用按钮防止重复提交
                $("#update_etl_task").attr({disabled: "disabled"});
            },
            //成功返回
            success: function (data) {
                $("#update_etl_task").removeAttr('disabled');
                if(data.code != '200'){
                    console.error(data.msg);
                    parent.layer.msg("执行失败");
                    return ;
                }
                parent.layer.msg(data.msg);
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
            },
            //处理完成
            complete: function () {
                console.info("complete")
            },
            //报错
            error: function (data) {
                $("#update_etl_task").removeAttr('disabled');
                layer.error(data.responseText);
                console.info("error: " + data.responseText);
            }
        });

    })

</script>

</body>

</html>
